// CLASS TESTPARSER USED TO TEST PARTS 2 AND 3 ONLY OF CPS710 ASSIGNMENT 2
//
class TestParser {

    private static SimpleCharStream instream;
    private static ParserTokenManager scanner;
    private static Parser parser;

    public static void main(String args[]) {

		// Figure out input stream from args or stdin
		java.io.InputStream infile;
		if (args.length < 1) {
	  	infile = System.in;
		} 
		else try {  
	   	infile = new java.io.FileInputStream(args[0]);
		} catch (java.io.FileNotFoundException e) {
	    	System.out.println("File " + args[0] + " not found."); 
	    	return;
		} 

		// Create scanner and parser
		instream = new SimpleCharStream(infile);
		scanner = new ParserTokenManager(instream);
		parser = new Parser(scanner);

		// Call parser until EOF reached
		while (nextParseSkipErrors());
    	}


    private static boolean nextParseSkipErrors() {
		try {
			Parser.ResetErrors();
			parser.start();
			if (Parser.AnyErrors())
				System.out.println("Parsing failed");
			else
				System.out.println("Parsing succeeded");

		// Handle Too Many Syntax Errors and EOF
		} catch (ParseAbortException e) {
			if (e.isEOF())	return false;
	    	System.out.println(e.toString());
		} catch (ParseException e) {
				/* Dummy: ParseException will be caught by start().
				 * However, JavaCC nevertheless automatically adds a "throws ParseException"
				 * to the function prototype of start().
				 * Therefore nextParseSkipErrors() must be seen to catch this exception		*/
		}
		return true;
	}
}
